import pandas as pd
import numpy as np

data = {
    "日期": pd.date_range("2024-01-01", periods=10, freq="D"),
    "产品": ["手机", "笔记本", "平板", "手机", "笔记本", "平板", "手机", "笔记本", "平板", "手机"],
    "销量": [100, 80, 60, 110, np.nan, 75, 95, 95, 70, 50],
    "单价": [3000, 5000, 2500, 3100, 4900, np.nan, 3050, 5200, 2600, 2950]
}

df = pd.DataFrame(data)

category = pd.DataFrame({
    "产品": ["手机", "笔记本", "平板"],
    "类别": ["通信设备", "计算机设备", "移动终端"]
})

# 保留日期列
df = df.reset_index()

# 合并
merged = pd.merge(df, category, on="产品", how="left")

# 恢复日期索引
merged["日期"] = pd.to_datetime(merged["日期"])
merged = merged.set_index("日期")

# 每3天汇总
ts = merged.resample("3D").sum(numeric_only=True)
print("每三天销售汇总：\n", ts)

# 移动平均与变化率
merged["移动平均销量"] = merged["销量"].rolling(window=3).mean()
merged["销售额变化率"] = merged["销售额"].pct_change()